using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace LANamp
{
    public partial class frmServerMain : Form
    {
        public frmServerMain()
        {
            InitializeComponent();
        }

        private LanAmpServer Ampserver;
        private string servername = "";
        public Form StartForm;
        public string Servername
        {
            set { if (servername == "") servername = value; }
            get { return servername; }
        }

        private void frmServerMain_Load(object sender, EventArgs e)
        {
            try
            {
                Ampserver = new LanAmpServer(this, Servername, Application.ProductVersion);
                Ampserver.ClientsUpdated += new ClientList.UpdateEvent(Ampserver_ClientsUpdated);
                Ampserver.Rx += new LanAmpServer.Transmission(Ampserver_Rx);
                Ampserver.Tx += new LanAmpServer.Transmission(Ampserver_Tx);
                Ampserver.SongUpdate += new LanAmpServer.SongUpdateEvent(Ampserver_SongUpdate);
            }
            catch
            {
                Close();
            }
            this.Text += " [" + Servername + "]";
            niServer.BalloonTipTitle += " - "+Servername;
            niServer.Text += " ["+Servername+"]";
            this.Left = Screen.PrimaryScreen.WorkingArea.Width - this.Width;
            this.Top = Screen.PrimaryScreen.WorkingArea.Height - this.Height;
            foreach (string arg in Environment.GetCommandLineArgs())
            {
                if (arg.Substring(0, 7) == "/index=")
                {
                    Ampserver.Playlist.IndexFolder(arg.Substring(7));
                }
            }
        }

        void Ampserver_SongUpdate(string SongInfo)
        {
            labSong.Text = SongInfo;
            labNextUser.Text = Ampserver.Clients.NextClientInfo;
            labSongCount.Text = Ampserver.Playlist.Count.ToString();
            int total = Ampserver.Clients.Count;
            int skipVote = Ampserver.Clients.SkipVoteCount;
            float perc = (total > 0) ?(float)skipVote / (float)total : (float)0;
            labSkipVotes.Text = skipVote.ToString() + " / " + total.ToString() + " (" + (perc).ToString("p0") + ")";
        }

        void Ampserver_ClientsUpdated(string Server)
        {
            lvClients.Items.Clear();
            lvClients.BeginUpdate();
            foreach (Client client in Ampserver.Clients.Items)
            {
                ListViewItem lvClient = lvClients.Items.Add(client.Nickname);
                lvClient.SubItems.Add(client.Host);
                lvClient.SubItems.Add(client.Version);
            }
            lvClients.EndUpdate();
        }
        private void Ampserver_Rx()
        {
            if (Visible)
            {
                Rx.BackColor = Color.Green;
                System.Timers.Timer RxTimer = new System.Timers.Timer();
                RxTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate(object o, System.Timers.ElapsedEventArgs e)
                {
                    Rx.BackColor = Color.Black;
                });
                RxTimer.AutoReset = false;
                RxTimer.Interval = 100;
                RxTimer.Start();
            }
        }

        private void Ampserver_Tx()
        {
            if (Visible)
            {
                Tx.BackColor = Color.Red;
                System.Timers.Timer TxTimer = new System.Timers.Timer();
                TxTimer.Elapsed += new System.Timers.ElapsedEventHandler(delegate(object o, System.Timers.ElapsedEventArgs e)
                {
                    Tx.BackColor = Color.Black;
                });
                TxTimer.AutoReset = false;
                TxTimer.Interval = 100;
                TxTimer.Start();
            }
        }

        private void frmServerMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            StartForm.Close();
        }

        private void frmServerMain_Shown(object sender, EventArgs e)
        {
            labServerName.Text = Ampserver.Name;
            labServerVersion.Text = Ampserver.Version;
            labServerWinamp.Text = Ampserver.WinampVersion;
            StartForm.Hide();
//            this.Hide();
        }

        private void labServerVersion_Click(object sender, EventArgs e)
        {

        }

        private void frmServerMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (Ampserver != null) Ampserver.Kill();
        }

        private void niServer_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.Visible = !this.Visible;
            if (this.Visible) this.Activate();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fb = new FolderBrowserDialog();
            fb.ShowNewFolderButton = false;
            if (fb.ShowDialog() == DialogResult.OK)
            {
                Ampserver.Playlist.IndexFolder(fb.SelectedPath);
                Ampserver.Playlist.Sort();
            }
        }
    }
}